package com.pt25.service.sys.repository.mysql.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pt25.base.dep.db.DbConstants;
import com.pt25.base.util.CollectionUtil;
import com.pt25.base.util.PageUtil;
import com.pt25.base.util.StrUtil;
import com.pt25.service.sys.domain.admin.ddo.AdminRole;
import com.pt25.service.sys.repository.mysql.converter.AdminConverter;
import com.pt25.service.sys.repository.mysql.mapper.AdminRoleMapper;
import com.pt25.service.sys.repository.mysql.persistence.AdminRoleRepo;
import com.pt25.service.sys.repository.mysql.po.AdminRolePo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class AdminRoleRepoImpl extends ServiceImpl<AdminRoleMapper, AdminRolePo> implements AdminRoleRepo {
    @Override
    public AdminRole getAdminRole(Long id) {
        return AdminConverter.adminRolePO2AdminRole(getById(id));
    }

    @Override
    public List<AdminRole> getAdminRolesByIds(Set<Long> ids) {
        List<AdminRole> adminRoles = new ArrayList<>();
        if (CollectionUtil.isEmpty(ids)) {
            return adminRoles;
        }
        QueryWrapper<AdminRolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(AdminRolePo::getId, ids);
        queryWrapper.lambda().eq(AdminRolePo::getDelFlag, DbConstants.DEL_FLAG0);
        list(queryWrapper).forEach(a -> adminRoles.add(AdminConverter.adminRolePO2AdminRole(a)));
        return adminRoles;
    }

    @Override
    public AdminRole getAdminRoleByRole(String role) {
        QueryWrapper<AdminRolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(AdminRolePo::getRole, role);
        queryWrapper.lambda().eq(AdminRolePo::getDelFlag, DbConstants.DEL_FLAG0);
        return AdminConverter.adminRolePO2AdminRole(getOne(queryWrapper));
    }

    @Override
    public PageUtil.PageVo pageAdminRole(AdminRolePo adminRolePO) {
        Page<AdminRolePo> page = new Page<>(adminRolePO.getPageNum(), adminRolePO.getPageSize());
        QueryWrapper<AdminRolePo> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(adminRolePO.getRole())) {
            queryWrapper.lambda().eq(AdminRolePo::getRole, adminRolePO.getRole());
        }
        baseMapper.selectPage(page, queryWrapper);
        return PageUtil.PageVo.builder()
                .total(page.getTotal())
                .pageNum(page.getCurrent())
                .pageSize(page.getSize())
                .list(page.getRecords())
                .build();
    }


}
